package com.tianpengtech.common;

import java.util.Properties;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.tianpengtech.admin.AdminRoutes;
import com.tianpengtech.common.generator.app.Generator;
import com.tianpengtech.common.handler.WebSocketHandler;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.dictionary.Dictionary;
import com.tianpengtech.common.model.permission.Permission;
import com.tianpengtech.common.model.permission.PermissionDispather;
import com.tianpengtech.common.model.role.Role;
import com.tianpengtech.common.plugin.shiro.ext.ShiroPlugin;
import com.tianpengtech.common.plugin.shiro.tag.ShiroTag;
import com.tianpengtech.common.util.DruidUtil;

public class MyConfig extends JFinalConfig {

    public Properties loadProp(String pro, String dev) {
        try {
            return loadPropertyFile(pro);
        } catch (Exception e) {
            return loadPropertyFile(dev);
        }
    }

    @Override
    public void configConstant(Constants me) {

        PropKit.use("a_little_config.txt");
        me.setDevMode(PropKit.getBoolean("devMode", false));
    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("contextPath"));// 设置上下文路路径 防止样式丢失
        me.add(new WebSocketHandler("^/websocket"));
        me.add(DruidUtil.getDruidStatViewHandler()); // druid 统计页面功能
    }

    @Override
    public void configInterceptor(Interceptors me) {

    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin = druidPluginConfig(me);
        ActiveRecordConfig(me, druidPlugin);
        shiroConfig(me);
    }

    /*
        配置DruidPlugin
     */
    private DruidPlugin druidPluginConfig(Plugins me) {
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
                PropKit.get("user"), PropKit.get("password").trim());
        WallFilter wallFilter = new WallFilter();              // 加强数据库安全
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(new StatFilter());
        me.add(druidPlugin);
        return druidPlugin;
    }

    /*
        配置Shiro
     */
    private void shiroConfig(Plugins me) {
        ShiroPlugin shiroPlugin = new ShiroPlugin(new AdminRoutes());
        shiroPlugin.setLoginUrl("/admin/login");
        shiroPlugin.setSuccessUrl("/admin/");
        shiroPlugin.setUnauthorizedUrl("/admin/forbidden");
        me.add(shiroPlugin);
    }

    /*

     */
    private void ActiveRecordConfig(Plugins me, DruidPlugin druidPlugin) {
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        me.add(arp);
        arp.addMapping(Account.dao.tableName(), Account.class);
        arp.addMapping(Role.dao.tableName(),Role.class);
        arp.addMapping(Dictionary.dao.tableName(),Dictionary.class);
        arp.addMapping(Permission.dao.tableName(),Permission.class);
        arp.addMapping(PermissionDispather.dao.tableName(),PermissionDispather.class);
        EhCachePlugin cachePlugin = new EhCachePlugin();
        me.add(cachePlugin);
    }

    @Override
    public void configRoute(Routes me) {

        me.add(new FrontRoutes());
        me.add(new AdminRoutes());
    }

    public void afterJFinalStart() {

    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8081, "/");
    }

    @Override
    public void configEngine(Engine me) {
        if(PropKit.getBoolean("devMode")){
            me.addSharedFunction("/WEB-INF/view/admin/macro/macro.html");
            me.addSharedFunction("/WEB-INF/view/admin/lay_out/__layout.html");
            me.addSharedStaticMethod(Generator.class);
            me.addSharedStaticMethod(ShiroTag.class);
            me.addSharedObject("AppName","xxxxx管理系统");
        }
    }
}
